package com.ee.chat.api.listener.mq;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.ee.chat.api.task.CallTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class CallFreeMQListener implements MessageListener {
    private Logger log = LoggerFactory.getLogger(CallFreeMQListener.class);

    @Resource
    CallTask callTask;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        log.info("通话定时收费消息");
        try {
            String body = new String(message.getBody(), "utf-8");
            log.info("消息内容："+body);
            Map<String, Object> map = JSONObject.parseObject(body, Map.class);

            //音视频通话扣费
            callTask.callFee(map);

            return Action.CommitMessage;
        }catch (Exception e){
            log.error("消息消费失败", e);
            return Action.ReconsumeLater;
        }
    }


}
